//
//  AddStoreViewController.m
//  iStore
//
//  Created by Geek on 15/5/8.
//  Copyright (c) 2015年 mirrtalk. All rights reserved.
//

#import "AddStoreViewController.h"
#import "LocationViewController.h"
#import "RequestEngine.h"

@interface AddStoreViewController () <UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>
{
    UIImage *storeImage;
    NSString *userID;
}
@end

@implementation AddStoreViewController

- (void) viewDidAppear:(BOOL)animated{

    NSString *addressText = [[NSUserDefaults standardUserDefaults] objectForKey:@"addRess"];
    NSString *name = [addressText substringFromIndex:3];
    
    [_confirButton setTitle:name forState:UIControlStateNormal];
    _confirButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _confirButton.contentEdgeInsets = UIEdgeInsetsMake(0,20, 0, 0);
    [_confirButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [sureButton setBackgroundImage:[UIImage imageNamed:@"Rectangle_6"] forState:UIControlStateHighlighted];
    [_choose setBackgroundImage:[UIImage imageNamed:@"Rectangle_6"] forState:UIControlStateHighlighted];
    

}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加店铺";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(goBacksActions)];
    userID = [[NSUserDefaults standardUserDefaults]objectForKey:kUserId];
    _registID.keyboardType = UIKeyboardTypeAlphabet;
}
- (void)goBacksActions
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark --跳转到定位按钮页面--

- (IBAction)confirmButton:(UIButton *)sender {
    LocationViewController * logVC = [[LocationViewController alloc]init];
    
    UINavigationController * navCtr = [[UINavigationController alloc]initWithRootViewController:logVC];
    
    [self presentViewController:navCtr animated:YES completion:nil];
    
    
}

- (IBAction)okButton:(UIButton *)sender {
    [self testFileDidFinish];

    NSString *buttonText = _confirButton.titleLabel.text;
    NSString *storeName = _storeName.text;
    NSString *registID = _registID.text;
    NSString *longtitude = [[NSUserDefaults standardUserDefaults] objectForKey:kLongtitude];
    NSString *latitude = [[NSUserDefaults standardUserDefaults] objectForKey:kLatitude];
    if (!storeImage) {
        alert(@"请选择营业执照");
        

        return;
    }
    if (!storeName.length || !storeName) {
        alert(@"请填写店铺名");
      

        return;
    }
    else if (storeName.length > 30) {
        alert(@"您的店名太长了吧");
        return;
    }
    if (!buttonText.length || !buttonText) {
        alert(@"请选择店址");
        

        return;
    }
    if (!registID.length || !registID) {
        
        alert(@"请填写营业执照号");
        

        return;
    }
    
    [self activityStart];
    [RequestEngine shopShopRegWithDic:@{@"userID":userID,
                                        @"shopAdress":buttonText,
                                        @"longtitude":longtitude,
                                        @"latitude":latitude,
                                        @"shopName":storeName,
                                        @"registID":registID,
                                        @"appKey":@"IOS"} imageArray:storeImage completed:^(NSString *errorcode, NSDictionary *resultDict) {
        if ([errorcode isEqualToString:@"0"]) {
            alert(@"信息已上传,请耐心等待审核");
            [self.navigationController popViewControllerAnimated:YES];
        }
        else if ([errorcode isEqualToString:@"-1"]){
            alert(@"请检查网络状况");
        }
        else{
            NSString *result = [NSString stringWithFormat:@"%@",resultDict];
            alert(result);
        }
        [self activityStop];
    }];
}

#pragma mark --选择照片
- (IBAction)chooseButton:(UIButton *)sender {
    
    UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相册" otherButtonTitles:@"拍照", nil];
    [actionSheet showInView:self.view];
    
}



- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    switch (buttonIndex) {
        case 0:
            //从相册提取
        {
            UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
            [imgPicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            [imgPicker setDelegate:self];
            [self presentViewController:imgPicker animated:YES completion:nil];
        }
            break;
            
            //拍照
        case 1:
        {
            UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
            [imgPicker setSourceType:UIImagePickerControllerSourceTypeCamera];
            [imgPicker setDelegate:self];
            [self presentViewController:imgPicker animated:YES completion:nil];
        }
            break;
    }
    
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    _imageView.image = image;
    storeImage = self.imageView.image;
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark --输入框上移--
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self testFileDidFinish];
}

- (void)testFileDidFinish
{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame =self.view.frame;
        frame.origin.y = 0.0;
        self.view.frame = frame;
    }];
    [self.view endEditing:YES];
}
- (IBAction)textBegan:(UITextField *)sender {
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = self.view.frame;
        frame.origin.y = -216;
        self.view.frame = frame;
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
